package com.book.redis.impl;

import com.book.dto.UserDto;
import com.book.redis.SessionProvider;
import com.book.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * @Author 李锦程
 * @Date 2021/3/28 7:52 下午
 * @Package com.book.redis.impl
 * @Version 1.0
 */
@Component
public class SessionProviderImpl implements SessionProvider {

    @Autowired
    private Jedis jedis;
    @Autowired
    private RedisUtil redisUtil;

    private Integer exp = 30;

    public void setExp(Integer exp) {
        this.exp = exp;
    }

    @Override
    public void setAttributeForUsername(String key, UserDto userDto) {
        //保存用户名到Redis中
        redisUtil.set(key,userDto);
        //时间
        jedis.expire(key, 60*exp);
    }

    @Override
    public UserDto getAttributeForUsername(String name) {
        UserDto userDto = (UserDto) redisUtil.get(name.getBytes());
        if(null != userDto){
            //时间
            jedis.expire(name, 60*exp);
        }
        return userDto;
    }
}
